package cn.bingoo.h2;

import static cn.bingoo.h2.domain.Tables.FRAMEDATA;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jooq.impl.Factory;
import org.jooq.util.h2.H2Factory;

import cn.bingoo.h2.domain.tables.records.FramedataRecord;
import cn.bingoo.handy.SshFrame;

public class H2Demo {
    public static void main(String[] args) throws SQLException {
        DriverManager.registerDriver(new org.h2.Driver());
        Connection conn = DriverManager.getConnection("jdbc:h2:~/bingoo-handy", "sa", "");
        DatabaseMetaData dbm = conn.getMetaData();
        ResultSet rs = dbm.getTables(null, null, "FRAMEDATA11", null);
        if (rs.next()) {
            System.out.println("Table exists");
        }
        else {
            System.out.println("Table deos not exist");
        }

        Factory create = new H2Factory(conn);
        //        FramedataRecord newRecord = create.newRecord(FRAMEDATA);
        //        newRecord.setFrameclass(SshFrame.class.getName());
        //        newRecord.setSavetime(new Timestamp(System.currentTimeMillis()));
        //        newRecord.setSettings("{JSON1}");
        //
        //        newRecord.store();
        //
        //        newRecord = create.newRecord(FRAMEDATA);
        //        newRecord.setFrameclass(SshFrame.class.getName());
        //        newRecord.setSavetime(new Timestamp(System.currentTimeMillis() + 10000));
        //        newRecord.setSettings("{JSON2}");
        //
        //        newRecord.store();

        FramedataRecord lastRecord =
                create.selectFrom(FRAMEDATA)
                        .where(FRAMEDATA.FRAMECLASS.equal(SshFrame.class.getName()))
                        .orderBy(FRAMEDATA.SAVETIME.desc())
                        .limit(1)
                        .fetchOne();
        System.out.println(lastRecord);
        // add application code here
        conn.close();
        DriverManager.deregisterDriver(new org.h2.Driver());
    }
}
